package com.salesys.gr.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.tony.domain.User;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class FrmManageVIPs {

	protected Shell FrmManageVIPs;
	private Text searchBox;
	private Label lblNickname;
	private Label lblMark;
	private Label lblMemberId;
	private Label lblQq;
	private Label lblEmail;
	private Label lblBankcard;
	private Label lblChildNum;
	private Label lblChildNumData;
	private Label lblNicknameData;
	private Label lblMarkData;
	private Label lblMemberIdData;
	private Label lblBankcardData;
	private Label lblEmailData;
	private Label lblQqData;
	private Button btnModifyInfo;
	private Button btnViewMarkRecord;
	private Button btnDeleteUser;
	private User operatingUser;
	private Label lblTip;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmManageVIPs.open();
		FrmManageVIPs.layout();
		while (!FrmManageVIPs.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmManageVIPs = new Shell(SWT.CLOSE | SWT.MIN | SWT.APPLICATION_MODAL);
		FrmManageVIPs.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 27) {
					FrmManageVIPs.close();
				}
			}
		});
		FrmManageVIPs.setSize(355, 325);
		FrmManageVIPs.setText("管理用户");
		// 窗口居中
		FrmManageVIPs.setLocation(new FrmHelper().setLocationCenter(FrmManageVIPs));

		searchBox = new Text(FrmManageVIPs, SWT.BORDER);
		searchBox.setBounds(10, 13, 100, 20);

		lblNickname = new Label(FrmManageVIPs, SWT.NONE);
		lblNickname.setBounds(45, 75, 61, 17);
		lblNickname.setText("姓　名：");

		lblMark = new Label(FrmManageVIPs, SWT.NONE);
		lblMark.setBounds(45, 100, 61, 17);
		lblMark.setText("现　金：");

		lblMemberId = new Label(FrmManageVIPs, SWT.NONE);
		lblMemberId.setBounds(45, 125, 61, 17);
		lblMemberId.setText("加盟卡号：");

		lblBankcard = new Label(FrmManageVIPs, SWT.NONE);
		lblBankcard.setBounds(45, 150, 61, 17);
		lblBankcard.setText("银行账号：");

		lblChildNum = new Label(FrmManageVIPs, SWT.NONE);
		lblChildNum.setBounds(45, 175, 61, 17);
		lblChildNum.setText("下级个数：");

		lblEmail = new Label(FrmManageVIPs, SWT.NONE);
		lblEmail.setBounds(45, 200, 61, 17);
		lblEmail.setText("邮　箱：");

		lblQq = new Label(FrmManageVIPs, SWT.NONE);
		lblQq.setBounds(45, 225, 61, 17);
		lblQq.setText("QQ：");

		lblNicknameData = new Label(FrmManageVIPs, SWT.NONE);
		lblNicknameData.setBounds(110, 75, 190, 17);

		lblMarkData = new Label(FrmManageVIPs, SWT.NONE);
		lblMarkData.setBounds(110, 100, 190, 17);

		lblMemberIdData = new Label(FrmManageVIPs, SWT.NONE);
		lblMemberIdData.setBounds(110, 125, 190, 17);

		lblBankcardData = new Label(FrmManageVIPs, SWT.NONE);
		lblBankcardData.setBounds(110, 150, 190, 17);

		lblChildNumData = new Label(FrmManageVIPs, SWT.NONE);
		lblChildNumData.setBounds(110, 175, 190, 17);

		lblEmailData = new Label(FrmManageVIPs, SWT.NONE);
		lblEmailData.setBounds(110, 200, 190, 17);

		lblQqData = new Label(FrmManageVIPs, SWT.NONE);
		lblQqData.setBounds(110, 225, 190, 17);

		Button btnUsernameSearch = new Button(FrmManageVIPs, SWT.NONE);
		btnUsernameSearch.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					if (searchBox.getText() == "") {
						new MsgBox().errorTip(FrmManageVIPs, "用户名或会员卡号不能为空");
					} else {
						try {
							User resultUser = new UserService().queryInfoFromUsername(searchBox.getText());
							if (resultUser.getUsername() == null) {
								new MsgBox().errorTip(FrmManageVIPs, "该用户不存在");
							} else {
								displayInfo(resultUser);
							}
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
				}
			}
		});
		btnUsernameSearch.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (searchBox.getText() == "") {
					new MsgBox().errorTip(FrmManageVIPs, "用户名或会员卡号不能为空");
				} else {
					try {
						User resultUser = new UserService().queryInfoFromUsername(searchBox.getText());
						if (resultUser.getUsername() == null) {
							new MsgBox().errorTip(FrmManageVIPs, "该用户不存在");
						} else {
							displayInfo(resultUser);
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			}
		});
		btnUsernameSearch.setBounds(120, 8, 100, 30);
		btnUsernameSearch.setText("按用户名搜索");

		Button btnMemberIdSearch = new Button(FrmManageVIPs, SWT.NONE);
		btnMemberIdSearch.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					if (searchBox.getText() == "") {
						new MsgBox().errorTip(FrmManageVIPs, "用户名或会员卡号不能为空");
					} else {
						try {
							User resultUser = new UserService().queryInfoFromMemberId(searchBox.getText());
							if (resultUser.getUsername() == null) {
								new MsgBox().errorTip(FrmManageVIPs, "该用户不存在");
							} else {
								displayInfo(resultUser);
							}
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
				}
			}
		});
		btnMemberIdSearch.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (searchBox.getText() == "") {
					new MsgBox().errorTip(FrmManageVIPs, "用户名或会员卡号不能为空");
				} else {
					try {
						User resultUser = new UserService().queryInfoFromMemberId(searchBox.getText());
						if (resultUser.getUsername() == null) {
							new MsgBox().errorTip(FrmManageVIPs, "该用户不存在");
						} else {
							displayInfo(resultUser);
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			}
		});
		btnMemberIdSearch.setBounds(230, 8, 110, 30);
		btnMemberIdSearch.setText("按加盟卡号搜索");

		btnModifyInfo = new Button(FrmManageVIPs, SWT.NONE);
		btnModifyInfo.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					if (searchBox.getText() == "") {
						new MsgBox().errorTip(FrmManageVIPs, "请先搜索用户");
					} else {
						FrmModifyInfo2 FrmModifyInfo2 = new FrmModifyInfo2();
						FrmModifyInfo2.transfer(operatingUser);
						FrmModifyInfo2.open();
						freshDisplayInfo();
					}
				}
			}
		});
		btnModifyInfo.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (searchBox.getText() == "") {
					new MsgBox().errorTip(FrmManageVIPs, "请先搜索用户");
				} else {
					FrmModifyInfo2 FrmModifyInfo2 = new FrmModifyInfo2();
					FrmModifyInfo2.transfer(operatingUser);
					FrmModifyInfo2.open();
					freshDisplayInfo();
				}
			}
		});
		btnModifyInfo.setBounds(10, 260, 80, 30);
		btnModifyInfo.setText("修改信息");

		btnViewMarkRecord = new Button(FrmManageVIPs, SWT.NONE);
		btnViewMarkRecord.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					if (searchBox.getText() == "") {
						new MsgBox().errorTip(FrmManageVIPs, "请先搜索用户");
					} else {
						FrmViewMarkRecord FrmViewMarkRecord = new FrmViewMarkRecord();
						FrmViewMarkRecord.transfer(operatingUser.getId());
						FrmViewMarkRecord.open();
					}
				}
			}
		});
		btnViewMarkRecord.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (searchBox.getText() == "") {
					new MsgBox().errorTip(FrmManageVIPs, "请先搜索用户");
				} else {
					FrmViewMarkRecord FrmViewMarkRecord = new FrmViewMarkRecord();
					FrmViewMarkRecord.transfer(operatingUser.getId());
					FrmViewMarkRecord.open();
				}
			}
		});
		btnViewMarkRecord.setBounds(109, 260, 130, 30);
		btnViewMarkRecord.setText("查看现金变更记录");

		btnDeleteUser = new Button(FrmManageVIPs, SWT.NONE);
		btnDeleteUser.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					if (searchBox.getText() == "") {
						new MsgBox().errorTip(FrmManageVIPs, "请先搜索用户");
					} else {
						boolean result = new MsgBox().questionTip(FrmManageVIPs, "确定要将该用户删除吗？");
						if (result == true) {
							try {
								new UserService().deleteUser(operatingUser.getId() + "", operatingUser.getUsername(), operatingUser.getParent());
							} catch (Exception e2) {
								e2.printStackTrace();
							}
						}
					}
				}
			}
		});
		btnDeleteUser.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (searchBox.getText() == "") {
					new MsgBox().errorTip(FrmManageVIPs, "请先搜索用户");
				} else {
					int affect = 0;
					boolean result = new MsgBox().questionTip(FrmManageVIPs, "确定要将该用户删除吗？");
					if (result == true) {
						try {
							affect = new UserService().deleteUser(operatingUser.getId() + "", operatingUser.getUsername(), operatingUser.getParent());
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					} else {
						affect = 2;
					}
					if (affect == 1) {
						new MsgBox().infoTip(FrmManageVIPs, "删除用户成功");
						searchBox.setText("");
					} else if (affect != 2) {
						new MsgBox().errorTip(FrmManageVIPs, "删除用户失败");
					}
				}
			}
		});
		btnDeleteUser.setBounds(259, 260, 80, 30);
		btnDeleteUser.setText("删除用户");

		lblTip = new Label(FrmManageVIPs, SWT.NONE);
		lblTip.setAlignment(SWT.CENTER);
		lblTip.setBounds(10, 45, 330, 17);
		lblTip.setText("请先输入用户名或加盟卡号搜索用户，然后管理该用户账号");
	}

	public void displayInfo(User resultUser) {
		operatingUser = new User(resultUser);
		if (operatingUser.getNickname() != null) {
			lblNicknameData.setText(operatingUser.getNickname());
		} else {
			lblNicknameData.setText("");
		}
		if (operatingUser.getMark() != null) {
			lblMarkData.setText(operatingUser.getMark());
		} else {
			lblMarkData.setText("0");
		}
		if (operatingUser.getMemberId() != null) {
			lblMemberIdData.setText(operatingUser.getMemberId());
		} else {
			lblMemberIdData.setText("非正式会员没有会员卡号");
		}
		if (operatingUser.getBankcard() != null) {
			lblBankcardData.setText(operatingUser.getBankcard());
		} else {
			lblBankcardData.setText("");
		}
		try {
			if (operatingUser.getChild() != null) {
				lblChildNumData.setText(new UserService().getChildNum(operatingUser.getChild()) + "");
			} else {
				lblChildNumData.setText("0");
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		if (operatingUser.getEmail() != null) {
			lblEmailData.setText(operatingUser.getEmail());
		} else {
			lblEmailData.setText("");
		}
		if (operatingUser.getQq() != null) {
			lblQqData.setText(operatingUser.getQq());
		} else {
			lblQqData.setText("");
		}
	}

	public void freshDisplayInfo() {
		try {
			User resultUser = new UserService().queryInfoFromUsername(operatingUser.getUsername());
			displayInfo(resultUser);
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
}
